setRetainInstance
Deprecated
Instead of retaining the Fragment itself, use a non-retained Fragment and keep retained state in a ViewModel attached to that Fragment. The ViewModel's constructor and its onCleared() callback provide the signal for initial creation and final destruction of the retained state.
Control whether a fragment instance is retained across Activity re-creation (such as from a configuration change). If set, the fragment lifecycle will be slightly different when an activity is recreated:
- onDestroy will not be called (but onDetach still will be, because the fragment is being detached from its current activity).
- onCreate will not be called since the fragment is not being re-created.
- onAttach and onActivityCreatedwill still be called.
Parameters
retain
true
to retain this fragment instance across configuration changes, false
otherwise.